var program =  {
		getclassrooms_list : function() {
		if(sessionStorage.getItem("suid")==null){
				return;
			}
		var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
		$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=assgetcr&address=http://103.37.166.4:7777/StateGrid/cra.do",
				scriptCharset : 'utf-8',
				success : function(param) {
					param = JSON.parse(param).data;
					program.createTable(param);
					program.list = param;
				},
				error : function() {
				}
			});
		},
		createTable : function(params){
			$("#getclassrooms_list").html("");
			var str = "";
			var count = 0;
			var j = 0;
			for ( var i in params) {
				if(params[i].alive=="1"){
					params[i].alive = "开启";
				}else if(params[i].alive=="0"){
					params[i].alive = "关闭";
				}
				if(params[i].isDel!="1"){
					j ++;
					var param = JSON.stringify(params[i]).replace(new RegExp('"','gm'),'|');
					if(i==0){
						str += '<tr class="active" id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td><td class="right green">'+params[i].alive+'</td></tr>';
						program.selectedRoom = params[i].ecdClassRoomId;
						program.selectedContent = params[i];
						createContent(param,'tr'+j);
					}else{
						str += '<tr id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td><td class="right green">'+params[i].alive+'</td></tr>';
					}
				}
			}
			$("#count").html(j);
			$(str).appendTo("#getclassrooms_list");
		},
		searchroom : function (){
			var condition = $("#selectCondition").val();
			var condition_str = $("#condition_str").val();
			if(condition_str == ""){
				program.createTable(program.list);
			}
			var new_list = program.mapping(condition,condition_str);
			program.createTable(new_list);
		},
		mapping : function (condition,condition_str){
			var list = [];
			j = 0;
			if(condition == "1"){
				for(var i in program.list){
					if(program.list[i].ecdClassRoomName.indexOf(condition_str)>-1){
						list[j] = program.list[i];
						j++;
					}
				}
			}else if(condition == "2"){
				for(var i in program.list){
					if(program.list[i].ecdClassRoomDescription.indexOf(condition_str)>-1){
						list[j] = program.list[i];
						j++;
					}
				}
			}
			return list;
		},
		selectedId : "tr1",
		selectedRoom : "",
		selectedContent: {},
		list : {}
	}

//查询演练室的所有事件

var getevents = {
	getevents_list : function() {
		console.info(program.selectedContent);
		$("#ecdName").html(program.selectedContent.ecdClassRoomName);
		$("#protName").html(program.selectedContent.projectName);
		$("#ecdDescription").html(program.selectedContent.ecdClassRoomDescription);
//		var userJson = JSON.parse(sessionStorage.getItem("suid")).data;
//		$("#login_name2").html(userJson.su_name);
		var crid = sessionStorage.getItem("crid");
		$
				.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getevents,crid="+ crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
					async : false,
					scriptCharset : 'utf-8',
					success : function(param) {
						param = JSON.parse(param).data;
						getevents.createTable(param);
//						alert(param);
						
					},
					error : function() {
					}
				});
	},
	createTable : function(params) {
		var str = "";
		var j = 0;
		var n = 0;
		for ( var i in params) {
			j++;
			str += '<div style="max-height: 162px;overflow-y: scroll;"><table class="Ptable" id="">'

			var s1 = "#a0a0ff";
			var s2 = "#ff9900";
			var s3 = "#ff3300";
			var s4 = "#0070c0";
			var s5 = "#ff00cc";
			var colors = [ {
				"color" : "#a0a0ff"
			}, {
				"color" : "#ff9900"
			}, {
				"color" : "#ff3300"
			}, {
				"color" : "#0070c0"
			}, {
				"color" : "#ff00cc"
			}, ]

			j = 0;
			for ( var k in params[i].ecdClassRoomEventPostList) {
				j++;
				n--
				var h = params[i].ecdClassRoomEventPostList.length + 1;
				if (k == 0) {
					str += '<tr><td rowspan="'
							+ h
							+ '" style="background:'+colors[i].color+';width: 1px;" class="'
							+ 's'
							+ j
							+ '"></td><td colspan="3" rowspan="'
							+ h
							+ '" style="width: 40px;" valign="top">'
							+ params[i].ceventName
							+ '</td><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】用户'
							+ j
							+ '</td><td><a href="javascript:;" class="addUser" id="editUser" onclick="editUser()"></a></td></tr>';
				} else {
					str += '<tr><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】用户'
							+ j
							+ '</td><td><a href="javascript:;" class="addUser" id="editUser" onclick="editUser()"></a></td></tr>';
				}
			}
			str += '</table></div>'
			console.info(params);

		}
		$(str).appendTo("#getevents_list");
		// $(str).appendTo("#getevents_pros")
	},

}

var getGuan = {
		getGuanbo : function(){
			var crid = sessionStorage.getItem("crid");
//			location.href="http://103.37.166.4:7777/StateGrid/guan00.jsp?crid="+crid; //本页面调转
			window.open('http://103.37.166.4:7777/StateGrid/guan00.jsp?crid='+crid+'"');//新页面跳转
		
		} 
}



function  createContent(param,id){
	$("#"+program.selectedId).removeClass('active');
	$("#"+id).addClass('active');
	program.selectedId = id;
	param = replaceAll(param);
	param = JSON.parse(param);
	program.selectedContent = param;
	program.selectedRoom = param.ecdClassRoomId;
	sessionStorage.setItem("crid",param.ecdClassRoomId);
	//获取演练室的信息
	$("#ecdClassRoomName").val(param.ecdClassRoomName);
	$("#projectName").val(param.projectName);
	$("#ecdClassRoomDescription").html(param.ecdClassRoomDescription);
}
$(function() {
	program.getclassrooms_list();
	$("#search").click(function(){
		program.searchroom();
	});
	$("#detail").click(function(){
		getevents.getevents_list();
	});
	$("#addJoin").click(function(){
		getGuan.getGuanbo();
	})
	
	
	
//	alert(new Date("2010/04/20 08:03:56"));
});
